package com.situ.day19;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.junit.Test;

import com.situ.day18.JDBCUtil;

public class Bank {
	@Test
	public void test1() {
		PreparedStatement preparedStatement = null;
		Connection connection = null;
		try {
			connection = JDBCUtil.getConnection();
			connection.setAutoCommit(false);
			String sql1 = "UPDATE point SET money = money - 1000 WHERE `name` = '张三';";
			preparedStatement = connection.prepareStatement(sql1);
			preparedStatement.executeUpdate();
			int num1 = 3 / 0;
			String sql2 = "UPDATE point SET money = money + 1000 WHERE `name` = '李四';";
			preparedStatement = connection.prepareStatement(sql2);
			preparedStatement.executeUpdate();
			connection.commit();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
	}
	@Test
	public void test2() {
		PreparedStatement preparedStatement = null;
		Connection connection = null;
		try {
			connection = JDBCUtil.getConnection();
			connection.setAutoCommit(false);
			String sql1 = "UPDATE point SET money = money - 1000 WHERE `name` = '张三';";
			preparedStatement = connection.prepareStatement(sql1);
			preparedStatement.executeUpdate();
			String sql2 = "UPDATE point SET money = money + 1000 WHERE `name` = '李四';";
			preparedStatement = connection.prepareStatement(sql2);
			preparedStatement.executeUpdate();
			connection.commit();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		
	}
	
	
	
	
}
